package co.edu.uniandes.moviles.rollerjudge;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import co.edu.uniandes.moviles.rollerjudge.logica.RollerJudge;
import co.edu.uniandes.moviles.rollerjudge.logica.Torneo;

public class InscribirEquiposActivity extends ListActivity implements OnItemClickListener {

	private ListView listTorneos;
	private Torneo[] nombresTorneos;

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		//        CheckedTextView item = (CheckedTextView) arg1;
		//    	TextView item = (TextView) arg1;
				Torneo selected = nombresTorneos[arg2];
		//		showDialog("Torneo Selccionado", selcted.toString());
		Intent intent = new Intent(this, SeleccionarEquiposActivity.class)
		.putExtra("torneo", arg2);
		startActivity(intent);
		//The change color logic is here!
		//        if(item.isChecked()) {
		//            item.setChecked(false);
		//        }
		//        else {
		//            item.setChecked(true);
		//        }

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inscribir_equipos);
		ArrayList<Torneo> torneos = RollerJudge.darInstancia().darTorneos();
		nombresTorneos = new Torneo[torneos.size()];
		int i = 0;
		for (Torneo torneo : torneos) {
			nombresTorneos[i] = torneo;
			i++;
		}
		ArrayAdapter<Torneo> adapter = new ArrayAdapter<Torneo>(this,
				android.R.layout.simple_list_item_1, nombresTorneos);

		getListView().setOnItemClickListener(this);

		setListAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@SuppressWarnings("deprecation")
	public void showDialog(String title, String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.setButton("Aceptar", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(final DialogInterface dialog, final int which) {				
				finish();
			}
		});
		alertDialog.show();
	}

}
